using System;
using Microsoft.Xna.Framework;

namespace Snake.Client
{
    public class Segment
    {
        public Segment(Vector2 position, Vector2 direction)
        {
            this.Direction = direction;
            this.Position = position;
        }

        public Vector2 Position { get; set; }
        public Vector2 Direction { get; set; }

        public void Move()
        {
            Position += Direction * (float) GameInfo.Speed;

            if (Position.X < 0)
            {
                Position = new Vector2(Screen.Width - GameInfo.SegmentSize, Position.Y);
            }
            else if (Position.X > Screen.Width - GameInfo.SegmentSize)
            {
                Position = new Vector2(0, Position.Y);
            }
            else if (Position.Y < 0)
            {
                Position = new Vector2(Position.X, Screen.Height - GameInfo.SegmentSize);
            }
            else if (Position.Y > Screen.Height - GameInfo.SegmentSize)
            {
                Position = new Vector2(Position.X, 0);
            }
        }
        public void DirectionByPrevious(Segment previous, int distanceModifier)
        {
            var segmentsPositionDifference = this.Position - previous.Position;
            Direction = -(segmentsPositionDifference / GameInfo.SegmentSize) * 20 / (distanceModifier + 19);
        }
        public bool CollidesWithSegment(Segment otherSegment)
        {
            var headSegmentDifference = (this.Position - otherSegment.Position);

            if (Math.Abs(headSegmentDifference.X) < GameInfo.SegmentSize &&
                    Math.Abs(headSegmentDifference.Y) < GameInfo.SegmentSize)
                {
                    return true;
                }
            return false;
        }
    }
}